﻿Public Class BanVe
    Private bus_BanVe As New BUS.BanVe
    Private dto_BanVe As New DTO.BanVe

    Private Sub BanVe_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Hide()
        e.Cancel = True
    End Sub

    Private Sub BanVe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        EAll(False)
        loaddto()
        cbsanbayden.DataSource = dto_BanVe.sb.Tables(0)
        cbsanbayden.DisplayMember = "Ten"
        cbsanbayden.ValueMember = "ID"
        cbsanbaydi.DataSource = dto_BanVe.sbd.Tables(0)
        cbsanbaydi.DisplayMember = "Ten"
        cbsanbaydi.ValueMember = "ID"
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Hide()
    End Sub

    Private Sub hvload(ByVal str As String)
        Dim hv As DataSet = bus_BanVe.GetHV(str)
        If Not IsNothing(hv) Then
            cbhangve.Enabled = True
            cbhangve.DataSource = hv.Tables(0)
            cbhangve.DisplayMember = "HangVe"
            cbhangve.ValueMember = "HangVe"
        End If
    End Sub

    Private Sub EAll(ByVal t As Boolean)
        cbmachuyenbay.Enabled = t
        cbhangve.Enabled = t
        Button1.Enabled = t
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim hv As DataSet = bus_BanVe.GetChuyenBay(cbsanbayden.SelectedValue, cbsanbaydi.SelectedValue, dtngay.Value.ToString("MM/dd/yyyy"))
        If hv.Tables(0).Rows.Count() = 0 Then
            MessageBox.Show("Không tìm thấy chuyến bay đạt yêu cầu", "Thông báo", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            EAll(False)
        Else
            cbmachuyenbay.Enabled = True
            cbmachuyenbay.DataSource = hv.Tables(0)
            cbmachuyenbay.DisplayMember = "ID"
            cbmachuyenbay.ValueMember = "ID"
            hvload(cbmachuyenbay.SelectedValue.ToString())
            Button1.Enabled = True
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        accept()
    End Sub

    Private Sub cbmachuyenbay_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbmachuyenbay.SelectedIndexChanged
        hvload(cbmachuyenbay.SelectedValue.ToString())

    End Sub

    Private Sub ttload(ByVal macb As String, ByVal mahv As String)
        Dim hv As DataSet = bus_BanVe.GetCBHV(macb, mahv)
        If Not IsNothing(hv) Then
            dto_BanVe.soghetrong = LaySoghe(Val(macb), Val(mahv))
            lbsanbaydi.Text = hv.Tables(0).Rows(0).Item(1).ToString()
            lbsanbayden.Text = hv.Tables(0).Rows(0).Item(2).ToString()
            lbngaykhoihanh.Text = hv.Tables(0).Rows(0).Item(3).ToString()
            lbthoigianbay.Text = hv.Tables(0).Rows(0).Item(4).ToString()
            lblghetrong.Text = dto_BanVe.soghetrong
            lblgia.Text = bus_BanVe.LayMaCBHV(Val(macb), Val(mahv)).Tables(0).Rows(0).Item(4).ToString + " USD"
        End If
        Dim sbtg As DataSet = bus_BanVe.GetSBTG(macb)
        If sbtg.Tables(0).Rows.Count > 0 Then
            Dim row As DataRow
            lbsanbaytg.Text = ""
            For Each row In sbtg.Tables(0).Rows
                lbsanbaytg.Text = lbsanbaytg.Text + row.Item(0)
                lbsanbaytg.Text = lbsanbaytg.Text + "; "
            Next
        Else
            lbsanbaytg.Text = "N/A"
        End If
    End Sub

    Private Sub cbhangve_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbhangve.SelectedIndexChanged
        ttload(cbmachuyenbay.SelectedValue.ToString(), cbhangve.SelectedValue.ToString())
    End Sub

    Private Function accept() As Boolean
        If Not Long.TryParse(txtcmndkh.Text, dto_BanVe.cmnd) Or txtcmndkh.Text = "" Then
            MessageBox.Show("Chứng minh thư nhân dân khách hàng không đúng!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Return False
        End If
        Dim cmexists As DataSet = bus_BanVe.GetCMND(txtcmndkh.Text)
        If rdnewhk.Checked = True Then
            dto_BanVe.hoten = txthotenkh.Text
            If dto_BanVe.hoten = "" Then
                MessageBox.Show("Chưa nhập họ tên khách hàng!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
                Return False
            End If
            If Not Long.TryParse(txtdienthoaikh.Text, dto_BanVe.dienthoai) Or txtdienthoaikh.Text = "" Then
                MessageBox.Show("Số điện thoại khách hàng không đúng!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
                Return False
            End If
            If cmexists.Tables(0).Rows.Count > 0 Then
                MessageBox.Show("Chứng minh thư nhân dân khách hàng đã tồn tại!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
                Return False
            Else
                If bus_BanVe.KTGheTrong(dto_BanVe.soghetrong) = False Then
                    MessageBox.Show("Không còn ghế trống của hạng vé này!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
                    Return False
                End If
                bus_BanVe.LuuKH(dto_BanVe)
                cmexists = bus_BanVe.GetCMND(txtcmndkh.Text)
                Dim cbhv As DataSet = bus_BanVe.LayMaCBHV(cbmachuyenbay.SelectedValue, cbhangve.SelectedValue)
                dto_BanVe.macb_hv = cbhv.Tables(0).Rows(0).Item(0)
                dto_BanVe.mahanhkhach = cmexists.Tables(0).Rows(0).Item(0)
                dto_BanVe.ngaymua = Today()
                dto_BanVe.giave = cbhv.Tables(0).Rows(0).Item(4)
                bus_BanVe.Muave(dto_BanVe)
                MessageBox.Show("Thành công!", "Thông báo", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation)
            End If
        Else
            If cmexists.Tables(0).Rows.Count = 0 Then
                MessageBox.Show("Chứng minh thư nhân dân khách hàng không tồn tại!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
                Return False
            Else
                cmexists = bus_BanVe.GetCMND(txtcmndkh.Text)
                Dim cbhv As DataSet = bus_BanVe.LayMaCBHV(cbmachuyenbay.SelectedValue, cbhangve.SelectedValue)
                dto_BanVe.macb_hv = cbhv.Tables(0).Rows(0).Item(0)
                dto_BanVe.mahanhkhach = cmexists.Tables(0).Rows(0).Item(0)
                dto_BanVe.ngaymua = Today()
                dto_BanVe.giave = cbhv.Tables(0).Rows(0).Item(4)
                bus_BanVe.Muave(dto_BanVe)
                MessageBox.Show("Thành công!", "Thông báo", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation)
            End If
        End If
        Return True
    End Function

    Private Sub loaddto()
        dto_BanVe.sb = bus_BanVe.GetSB()
        dto_BanVe.sbd = bus_BanVe.GetSB()
    End Sub


    Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        If rdoldhk.Checked = True Then
            Dim cmexists As DataSet = bus_BanVe.GetCMND(txtcmndkh.Text)
            If cmexists.Tables(0).Rows.Count = 0 Then
                MessageBox.Show("Chứng minh thư nhân dân khách hàng không tồn tại!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
                txtdienthoaikh.Text = ""
                txthotenkh.Text = ""
            Else
                txthotenkh.Text = cmexists.Tables(0).Rows(0).Item(1)
                txtdienthoaikh.Text = cmexists.Tables(0).Rows(0).Item(2)
            End If
        Else
            MessageBox.Show("Khách hàng mới không cần kiểm tra thông tin!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
        End If
    End Sub

    Private Function LaySoghe(ByVal macb As Integer, ByVal mahv As Integer) As Integer
        Dim dtsoghe As DataSet = bus_BanVe.LayMaCBHV(macb, mahv)
        Dim soghe As Integer = dtsoghe.Tables(0).Rows(0).Item(3)
        Dim macbhv As Integer = dtsoghe.Tables(0).Rows(0).Item(0)
        Return soghe - bus_BanVe.DemSoGheDat(macbhv)
    End Function
End Class